package cn.org.timespace.ons.dao.base.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.ons.action.base.MomModel;
import cn.org.timespace.ons.dao.TSBaseDAO;
import cn.org.timespace.ons.dao.base.MomDao;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.vo.base.Mom;
import cn.org.timespace.ons.vo.base.Options;

public class MomDaoImpl  extends TSBaseDAO implements MomDao{
	
	

	@Override
	public List<Mom> findCompCodeAndDeviceCode(String port,String ipAddress) {
		
		log.debug("finding all findCompCodeAndDeviceCode instances");
	    String hql = "from Mom m where port='"+port+"' and ipAddress='"+ipAddress+"'";
		
		return (List<Mom>) this.getHibernateTemplate().find(hql);
	}
	
	@Override
	public Mom findById(String momid) {
	
			// TODO Auto-generated method stub
			log.debug("getting Mom instance with id: " +momid);
			try{
				Mom instance = (Mom)getHibernateTemplate().get(
						"cn.org.timespace.ons.vo.base.Mom", momid);
				return instance;
			}catch(RuntimeException re){
				log.debug("get fail",re);
				throw re;
		}
	}
	
	@Override
	public void save(Mom transientInstance) {
	
		log.debug("saving Mom Instance");
		try{
			getHibernateTemplate().save(transientInstance);
			log.debug("saving successful");
		}catch(RuntimeException re){
			log.debug("saving fail",re);
			throw re;
		}
	}

	@Override
	public void delete(Mom persistentInstance) {
		log.debug("deleting Mom Instance");
		try{
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		}catch(RuntimeException re){
			log.debug("delete fail",re);
			throw re;
		}
	}
	
	@Override
	public void update(Mom instance) {
		
		log.debug("update Mom Instance");
		try{
			getHibernateTemplate().update(instance);
			
		}catch(RuntimeException re){
			log.error("update failed!",re);
			throw re;
		}	
	}

	@Override
	public List<Mom> findByCondition(MomModel model) {
		
		log.debug("findByCondition Mom Instance");
		StringBuffer queryString = new StringBuffer("from Mom m where 1=1");
		if(!StringUtils.isEmpty(model.getCompCode())){
			queryString.append("and m.compCode like '%" + model.getCompCode() + "'%" );
		}
		if(!StringUtils.isEmpty(model.getIpAddress())){
			queryString.append("and m.ipAddress like '%" + model.getIpAddress() + "'%" );
		}
		if(!StringUtils.isEmpty(model.getPort())){
			queryString.append("and m.port like '%" + model.getPort() + "'%" );
		}
		queryString.append(" order by m.momid desc");
		
		
		return getHibernateTemplate().find(queryString.toString());
	}

	@Override
	public List<Options> findByCode(String code) {
		
		log.debug("getting Options instance with code");
	    String hql = "from Options o where code='"+code+"'";
		
	    return (List) this.getHibernateTemplate().find(hql);
	}

	@Override
	public List findByName(String name) {
		
		log.debug("getting Options instance with name");
	    String hql = "from Options o where name='"+name+"'";
		
	    return (List) this.getHibernateTemplate().find(hql);
	}

	@Override
	public List findByTheId(String theId) {
		
		log.debug("finding all findByTheId instances");
	    String hql = "select o.name from Options as o,Mom as m where o.theId=m.type and o.theId='"+theId+"'";
	 
	    System.out.println(hql);
	    return (List) this.getHibernateTemplate().find(hql);
	}
	
	@Override
	public List<Object> findByCondition(MomModel model, Pager p) {

	log.debug("finding all findByCondition instances");
		
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("select m.dqM,m.compCode,m.compRandom,m.deviceCode,m.deviceRandom,m.name,m.status,m.address,m.remark,m.momid,m.port,m.ipAddress,m.dns from Mom m where 1=1 ");//去掉关联and mom1_.type=options0_.theID
			if (!StringUtils.isEmpty(model.getCompCode())) {
				queryString.append(" and m.compCode like '%" + model.getCompCode().trim()
						+ "%'");
			}
			if (!StringUtils.isEmpty((model.getStatus())+"")) {
				queryString.append(" and m.status = '" + model.getStatus()+""+"'");
			}
			queryString.append(" order by m.momid desc");
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			List<Object> resList = new ArrayList();
			resList.add(query.list().size());
			
			setQueryPager(query, p);
//			resList.add(query.list());
			
			List list = new ArrayList();
			for(int i = 0;i < query.list().size();i++){
				Object[] obj=(Object[])query.list().get(i);
				Mom mom = new Mom();
				mom.setType(obj[0].toString());
				//mom.setCompCode(obj[1].toString());
				//mom.setCompRandom(obj[2].toString());
				//mom.setDeviceCode(obj[3].toString());
				//mom.setDeviceRandom((obj[4].toString()));
				mom.setName((obj[5].toString()));
				mom.setStatus(Integer.parseInt(obj[6].toString()));
				if(obj[7] != null){
					mom.setAddress(obj[7].toString());
				}
				
				mom.setRemark(obj[8].toString());
				mom.setMomid((obj[9].toString()));
				mom.setPort((obj[10].toString()));
				mom.setIpAddress((obj[11].toString()));
				mom.setDns((obj[12].toString()));
				list.add(mom);
			}
			resList.add(list);
			return resList;
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSessionAndQuery(session, query);
		}
	}
}
